package com.shuzijun.leetcode.editor.cn;
public class FindFirstAndLastPositionOfElementInSortedArray{
    public static void main(String[] args) {
         Solution solution = new FindFirstAndLastPositionOfElementInSortedArray().new Solution();
//       solution.searchRange(new int[]{5,7,7,8,8,10},8) ;
       String x = "1455284512393099";
        System.err.println(x.length());
    }
    //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int[] searchRange(int[] nums, int target) {
        int low = 0;
        int high = nums.length-1;
        int left = -1;

       while (low <= high){
           int mid = (low + high) / 2;
           if (nums[mid] == target){
               if (mid == 0 || nums[mid-1] != target){
                   left = mid;
                   break;
               }else {
                   high = mid-1;
               }
           } else if (nums[mid] > target) {
               high = mid-1;
           }else {
               low = mid+1;
           }
       }

       low = 0;
       high = nums.length-1;
       int right = -1;
       while (low <= high){
           int mid = (low + high) / 2;
           if (nums[mid] == target){
               if (mid == nums.length-1 || nums[mid+1] != target){
                   right = mid;
                   break;
               }else {
                   low = mid+1;
               }
           } else if (nums[mid] > target) {
               high = mid-1;
           }else {
               low = mid+1;
           }
       }
       return new int[]{left,right};

    }
}

//leetcode submit region end(Prohibit modification and deletion)

}